package com.fh.shop.api.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {

    public static String sendPost(String url, Map<String, String> headers, Map<String, String> params) {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        try {
            // 传递头信息
            if (headers != null && headers.size() > 0) {
    //            Iterator<Map.Entry<String, String>> iterator = headers.entrySet().iterator();
    //            while (iterator.hasNext()) {
    //                Map.Entry<String, String> next = iterator.next();
    //                httpPost.addHeader(next.getKey(), next.getValue());
    //            }
                // lambda实现同样的效果
                headers.forEach((x, y) -> httpPost.addHeader(x, y));
            }
            // 传递参数
            if (params != null && params.size() > 0) {
                List<NameValuePair> pairList = new ArrayList<>();
//                Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
//                while (iterator.hasNext()) {
//                    Map.Entry<String, String> next = iterator.next();
//                    pairList.add(new BasicNameValuePair(next.getKey(), next.getValue()));
//                }
                params.forEach((x, y) -> pairList.add(new BasicNameValuePair(x, y)));
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairList, "utf-8");
                httpPost.setEntity(urlEncodedFormEntity);
            }
            // 发送请求
            response = client.execute(httpPost);
            // 获取响应
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "utf-8");
            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != httpPost) {
                httpPost.releaseConnection();
            }

            if (null != client) {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
